﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDD.Shared.DAC;
using TDD.Shared.Models;

namespace TDD.Shared.Engines
{
    public class LoginEngine
    {
        private IDataAccess _dac;

        public LoginEngine(IDataAccess dac)
        {
            _dac = dac;
        }

        public bool CheckLogin(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false;

            var qry = _dac.GetAllUsers();
            return qry.Any
                (it =>
                    it.Username.Equals(username, StringComparison.CurrentCultureIgnoreCase)
                    && it.Password.Equals(password, StringComparison.CurrentCultureIgnoreCase)
                );

            //foreach (var item in qry)
            //{
            //    if((item.Username.ToLower().Equals(username.ToLower())) && (item.Password.ToLower().Equals(password.ToLower())))
            //    {
            //        return true;
            //    }
            //}
            //return false;
        }

        public UserInformation GetUserInformation(int id)
        {
            return _dac.GetUserInfomationById(id);
        }

        public void ChangePassword(int id, string newPassword, string oldPassword)
        {
            var account = _dac.GetUserInfomationById(id);

            if (oldPassword.Equals(account.Password))
            {
                account.Password = newPassword;
                _dac.EditUserInformation(account);
            }
        }
    }
}
